package logic

import (
	"context"
	"fmt"
	"home-manage/homerpc/homerpc"

	"home-manage/api/internal/svc"
	"home-manage/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type HoseAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewHoseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HoseAddLogic {
	return &HoseAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *HoseAddLogic) HoseAdd(req *types.HouseAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	if req.Num == 0 || req.Lease == "" || req.Deposit == "" || req.Title == "" || req.Facility == "" || req.Money == 0 {
		return nil, fmt.Errorf("参数不能为空")
	}
	add, err := l.svcCtx.HomeRpc.HouseAdd(l.ctx, &homerpc.HouseAddRequest{
		UserId:   req.UserId,
		Title:    req.Title,
		Lease:    req.Lease,
		Money:    float32(req.Money),
		Deposit:  req.Deposit,
		Num:      float32(req.Num),
		Facility: req.Facility,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Data:    nil,
			Code:    0,
		}, nil
	}
	return &types.Response{
		Message: "添加成功",
		Data:    add,
		Code:    200,
	}, nil
}
